/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewTaskGUI.java
 *
 * Created on Jun 18, 2009, 12:18:23 PM
 */

package GUI;

import BUS.Task2BUS;
import DTO.Task2DTO;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author trquanglong
 */
public class NewTaskGUI extends javax.swing.JDialog {

    private Task2DTO _task = new Task2DTO();
    private boolean _change = false;

    /** Creates new form NewTaskGUI */
    public NewTaskGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        txtEndDay.setText("");
        txtStartDay.setText("");
        txtOwner.setText("");
        txtSubject.setText("");
        cmbComplete.removeAllItems();
        for (int i = 0; i <= 10; i ++)
            cmbComplete.addItem(Integer.toString((i) * 10) + "%");
        cmbPriority.removeAllItems();
        cmbPriority.addItem("Normal");
        cmbPriority.addItem("Very low");
        cmbPriority.addItem("Low");
        cmbPriority.addItem("High");
        cmbPriority.addItem("Very high");
        cmbPriority.setSelectedIndex(0);

        cmbStatus.removeAllItems();;
        cmbStatus.addItem("Not start");
        cmbStatus.addItem("Started");
        cmbStatus.addItem("End");
        cmbStatus.setSelectedIndex(0);

        this.setTitle("New Task");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtSubject = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtStartDay = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtEndDay = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cmbStatus = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        cmbPriority = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        txtOwner = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        cmbComplete = new javax.swing.JComboBox();
        bttnAdd = new javax.swing.JButton();
        bttnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Subject");

        txtSubject.setText("jTextField1");

        jLabel2.setText("Start Day");

        txtStartDay.setText("jTextField2");

        jLabel3.setText("End day");

        txtEndDay.setText("txtEndDay");

        jLabel4.setText("Status");

        cmbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel5.setText("Priority");

        cmbPriority.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel7.setText("Owner");

        txtOwner.setText("jTextField2");

        jLabel6.setText("Complete");

        cmbComplete.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        bttnAdd.setText("ADD");
        bttnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttnAddActionPerformed(evt);
            }
        });

        bttnCancel.setText("CANCEL");
        bttnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel7))
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cmbStatus, javax.swing.GroupLayout.Alignment.LEADING, 0, 193, Short.MAX_VALUE)
                            .addComponent(txtStartDay, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
                        .addGap(41, 41, 41)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5))
                                .addGap(23, 23, 23)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cmbPriority, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtEndDay, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(bttnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(bttnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(txtSubject, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                    .addComponent(txtOwner, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmbComplete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(441, 441, 441))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtStartDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtEndDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cmbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbPriority, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(txtOwner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(cmbComplete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(bttnAdd)
                        .addComponent(bttnCancel)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bttnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttnCancelActionPerformed
        // TODO add your handling code here:
        _task = null;
        this.dispose();
    }//GEN-LAST:event_bttnCancelActionPerformed

    private void bttnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttnAddActionPerformed
        // TODO add your handling code here:
        if (txtSubject.getText() .equals("")){
            JOptionPane.showMessageDialog(cmbComplete, "Please type subject of task!", "Requirement", JOptionPane.ERROR_MESSAGE);
            return;
        }else{
            int trueFormat = 0;
            _task.setSubject(txtSubject.getText());
            String startDay = txtStartDay.getText();
            String endDay = txtEndDay.getText();
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
            try {
                df.parse(startDay);
                df.parse(endDay);
                trueFormat = 1;
            } catch (ParseException ex){
                JOptionPane.showMessageDialog(null, "Day must in 'dd/MM/yyyy'");
            }
            if(trueFormat == 0)
                return;
            _task.setStartedDay(txtStartDay.getText());
            _task.setComplete(cmbComplete.getSelectedIndex() * 10);
            _task.setPriority(cmbPriority.getSelectedItem().toString());
            _task.setStatus(cmbStatus.getSelectedItem().toString());
            _task.setOwner(txtOwner.getText());
            _task.setEndDay(txtEndDay.getText());
            Task2BUS.addTaskToDB(_task);
            _change = true;
            int option = JOptionPane.showConfirmDialog(null, "Add new task successfull \nDo you want to continue to create a new task?", "Confirm", JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION)
            {
                txtEndDay.setText("");
                txtStartDay.setText("");
                txtOwner.setText("");
                txtSubject.setText("");
                cmbComplete.removeAllItems();
                for (int i = 0; i <= 10; i ++)
                    cmbComplete.addItem(Integer.toString((i) * 10) + "%");
                cmbPriority.removeAllItems();
                cmbPriority.addItem("Normal");
                cmbPriority.addItem("Very low");
                cmbPriority.addItem("Low");
                cmbPriority.addItem("High");
                cmbPriority.addItem("Very high");
                cmbPriority.setSelectedIndex(0);

                cmbStatus.removeAllItems();;
                cmbStatus.addItem("Not start");
                cmbStatus.addItem("Started");
                cmbStatus.addItem("End");
                cmbStatus.setSelectedIndex(0);
                this.setTitle("New Task");
            }else{
                this.dispose();
            }
        }
    }//GEN-LAST:event_bttnAddActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewTaskGUI dialog = new NewTaskGUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bttnAdd;
    private javax.swing.JButton bttnCancel;
    private javax.swing.JComboBox cmbComplete;
    private javax.swing.JComboBox cmbPriority;
    private javax.swing.JComboBox cmbStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtEndDay;
    private javax.swing.JTextField txtOwner;
    private javax.swing.JTextField txtStartDay;
    private javax.swing.JTextField txtSubject;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the _task
     */
    public Task2DTO getTask() {
        return _task;
    }

    /**
     * @param task the _task to set
     */
    public void setTask(Task2DTO task) {
        this._task = task;
    }

    /**
     * @return the _change
     */
    public boolean isChange() {
        return _change;
    }

    /**
     * @param change the _change to set
     */
    public void setChange(boolean change) {
        this._change = change;
    }

}
